package com.linghang.wusthelpercompetition.service.impl;

import com.github.houbb.sensitive.word.core.SensitiveWordHelper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.linghang.wusthelpercompetition.constant.StatusConstant;
import com.linghang.wusthelpercompetition.dto.ResponseDTO;
import com.linghang.wusthelpercompetition.dto.ResponsePageDTO;
import com.linghang.wusthelpercompetition.dto.ResponseUpdateDTO;
import com.linghang.wusthelpercompetition.entity.CompetitionPost;
import com.linghang.wusthelpercompetition.entity.ResponsePost;
import com.linghang.wusthelpercompetition.mapper.ResponsePostMapper;
import com.linghang.wusthelpercompetition.result.PageResult;
import com.linghang.wusthelpercompetition.service.ResponsePostService;
import com.linghang.wusthelpercompetition.utils.TencentTmsUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class ResponsePostServiceImpl implements ResponsePostService {
    @Autowired
    private ResponsePostMapper responsePostMapper;
    @Autowired
    private TencentTmsUtils tencentTmsUtils;

    /**
     * 新增回复信息
     * @param responseDTO
     */
    @Override
    public void addResponse(ResponseDTO responseDTO) {
//        String response = responseDTO.getResponse();
//        if(SensitiveWordHelper.contains(response)){
//            throw new RuntimeException("回复内容包含敏感词");
//        }
//        log.info("是否包含敏感词"+SensitiveWordHelper.contains(response));
        ResponsePost responsePost = new ResponsePost();
        responsePost.setResponse(responseDTO.getResponse());
        responsePost.setCid(responseDTO.getCid());
        responsePost.setStudentId(responseDTO.getStudentId());
        responsePost.setStatus(StatusConstant.ENABLE);
        responsePost.setEndUpdateTime(LocalDateTime.now());
        //联系方式
        responsePost.setContactInformation(responseDTO.getContactInformation());
        responsePostMapper.insert(responsePost);
    }

    /**
     * 删除回复信息
     * @param rid
     */
    @Override
    public void deleteResponse(Long rid) {
        responsePostMapper.deleteById(rid);
    }

    /**
     * 根据rid查询回复信息
     * @param rid
     * @return ResponsePost
     */
    @Override
    public ResponsePost getResponseById(Long rid) {
        return responsePostMapper.selectById(rid);
    }

//    /**
//     * 根据学号studentId查询所有回复
//     * @param studentId
//     * @return List<ResponsePost>
//     */
//    @Override
//    public List<ResponsePost> listMyResponses(String studentId) {
//        return responsePostMapper.selectListByStudentId(studentId);
//    }

    /**
     * 根据帖子cid查询所有回复
     * @param cid
     * @return List<ResponsePost>
     */
    @Override
    public List<ResponsePost> listResponsesByPost(Long cid) {
        return responsePostMapper.selectListByCid(cid);
    }

    /**
     * 更新回复信息
     * @param responseUpdateDTO
     * @return
     */
    @Override
    public void updateResponse(ResponseUpdateDTO responseUpdateDTO) {

        ResponsePost responsePost = new ResponsePost();
        responsePost.setRid(responseUpdateDTO.getRid());
        responsePost.setResponse(responseUpdateDTO.getResponse());
        responsePost.setEndUpdateTime(LocalDateTime.now());
        //状态
        responsePost.setStatus(responseUpdateDTO.getStatus());
        //联系方式
        responsePost.setContactInformation(responseUpdateDTO.getContactInformation());
        responsePostMapper.updateById(responsePost);
    }

    /**
     * 处理回复信息
     * 直接调用updateById即可，可复用
     * @param rid
     */
    @Override
    public void handleResponse(Long rid) {
        ResponsePost responsePost = responsePostMapper.selectById(rid);
        responsePost.setStatus(StatusConstant.DISABLE);
        responsePostMapper.updateById(responsePost);
    }

    /**
     * 分页查询回复信息(根据cid查询或者根据studentId查询)
     * cid和studentId不能同时不为空
     * @param responsePageDTO
     * @return PageResult
     */
    @Override
    public PageResult pageByCidOrStudentId(ResponsePageDTO responsePageDTO) {
        PageHelper.startPage(responsePageDTO.getPage(),responsePageDTO.getPageSize());
        List<ResponsePost>list = null;
        if(responsePageDTO.getCid() != null){
            list = responsePostMapper.selectListByCid(responsePageDTO.getCid());
        }
        else {
            list = responsePostMapper.selectListByStudentId(responsePageDTO.getStudentId());
        }
        PageInfo<ResponsePost> pageInfo = new PageInfo<>(list);
        Long total = pageInfo.getTotal();
        List<ResponsePost> responsePosts = pageInfo.getList();

        return new PageResult(total,responsePosts);
    }

    /**
     * 根据rid查询cid
     * @param rid
     * @return
     */
    @Override
    public Long getCidByRid(Long rid) {
        return responsePostMapper.selectById(rid).getCid();
    }

    @Override
    public ResponsePost getByCidAndStudentId(Long cid, String studentId) {
        return responsePostMapper.getResponsePostByCidAndStudentId(cid,studentId);
    }

    /**
     * 根据studentID查询cid集合
     * @param studentId
     * @return
     */
    @Override
    public List<Long> selectCidsByStudentId(String studentId) {
        List<ResponsePost> list = responsePostMapper.selectCidsByStudentId(studentId);
        List<Long> cids = new ArrayList<>();
        list.forEach(responsePost -> {
            cids.add(responsePost.getCid());
        });

        return cids;
    }

    @Override
    public Long getTotalByStudentId(String studentId) {
        return responsePostMapper.getTotalByStudentId(studentId);
    }
}
